package com.vhall.component.controller.chat;

import com.vhall.component.controller.chat.facade.ChatConsoleFacade;
import com.vhall.component.entity.chat.vo.ChatConsoleListPage;
import com.vhall.component.entity.chat.vo.ChatConsoleListReqVO;
import com.vhall.component.entity.chat.vo.ChatConsoleListRspVO;
import com.vhall.component.entity.chat.vo.ExportMessReqVO;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.constant.CommonConstant;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.plugin.core.service.TokenService;
import com.vhall.component.service.chat.ChatConsoleService;
import com.vhall.component.service.room.RoomInnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author guoxin.wang
 * @date 2021/5/28 10:15
 */
@Slf4j
@VhallComponent
public class ChatConsoleController implements ChatConsoleFacade {

    @Autowired
    TokenService tokenService;

    @Autowired
    private RoomInnerService roomInnerService;

    @Autowired
    private ChatConsoleService chatConsoleService;

    @Override
    @TokenAuth
    public Boolean export(ExportMessReqVO reqVO) {

        Token accountInfo = TokenThreadLocal.getToken();
        String roomId = reqVO.getRoomId();
        RoomsEntityDTO roomInfo = roomInnerService.getRoomInfo(roomId);
        if (null == roomInfo) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
        String beginTime = reqVO.getBeginTime() == null ? "2020-01-01" : reqVO.getBeginTime();
        String endTime = reqVO.getEndTime();
        Integer filterStatus = reqVO.getFilterStatus() == null ? 0 : reqVO.getFilterStatus();
        Integer accountId = accountInfo.getAccountId();
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern(CommonConstant.SIMPLE_DATETIME_FORMAT);
        Date date = new Date();
        String fileName = "StatRoomsMessageList" + sdf.format(date) + accountInfo.getAccountId();
        return chatConsoleService.exportMessage(roomInfo.getIlId(), accountId, fileName, beginTime, endTime, filterStatus, roomInfo);
    }

    @Override
    public ChatConsoleListPage<ChatConsoleListRspVO> list(ChatConsoleListReqVO reqVO) {
//        Token accountInfo = TokenThreadLocal.getToken();
        String roomId = reqVO.getRoomId();
        RoomsEntityDTO roomInfo = roomInnerService.getRoomInfo(roomId);
        if (null == roomInfo) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
        // 校验一下房间信息中的account_id和根据token解析出来的account_id是否一致。
        // 3、校验一下用户信息里的account_id和room里的account_id是否相等。
//        if (!(StringUtils.isNotBlank(String.valueOf(accountInfo.getAccountId())) && ObjectUtils.isNotEmpty(roomInfo.getAccountId()) && String.valueOf(accountInfo.getAccountId()).equals(String.valueOf(roomInfo.getAccountId())))) {
//            log.error("用户信息中的account_id和房间信息中的account_id不匹配！:{},{}", accountInfo.getAccountId(), roomInfo.getAccountId());
//            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
//        }
        // 防止前端不传递分页参数，在这里进行设置。
        if (reqVO.getPage() == null) {
            reqVO.setPage(1);
        }
        if (reqVO.getPageSize() == null) {
            reqVO.setPageSize(20);
        }
        return chatConsoleService.list(reqVO, roomInfo);
    }
}
